package com.example.zluserservice.framework.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.service.ApiInfo;

import springfox.documentation.service.RequestParameter;
import springfox.documentation.builders.RequestParameterBuilder;
import springfox.documentation.service.ParameterType;
import springfox.documentation.schema.ScalarType;
import springfox.documentation.service.Contact;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.spi.DocumentationType;

import springfox.documentation.oas.annotations.EnableOpenApi;

import java.util.Collections;


/**
 * @Author zhenliu（孙凌岳）
 * @Description AI生成 - Swagger3.0.3配置
 * @Date 2025/6/15 12:05
 * @Version 1.0
 */
@Configuration
@EnableOpenApi
public class SwaggerConfig {

    @Bean
    public Docket userApi() {
        return new Docket(DocumentationType.OAS_30)
                .groupName("用户服务接口")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.zluserservice.controller"))
                .paths(PathSelectors.any())
                .build()
                .globalRequestParameters(Collections.singletonList(tokenHeader())); // 添加全局请求头参数
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("用户服务 API 文档")
                .description("提供用户服务相关接口的文档说明")
                .version("1.0.0")
                .contact(new Contact("zhenliu（孙凌岳）", " ", "23684100997@qq.com"))
                .build();
    }

    /**
     * 设置全局通用请求头：Authorization
     */
    private RequestParameter tokenHeader() {
        return new RequestParameterBuilder()
                .name("Authorization")
                .description("JWT认证令牌，格式：Bearer {token}")
                .required(false)
                .in(ParameterType.HEADER)
                .query(q -> q.model(m -> m.scalarModel(ScalarType.STRING)))
                .build();
    }
}
